﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model = eShop.Model;
using Entity = eShop.DataAccess.Entity;
using eShop.Model;

namespace eShop.DataAccess.Converter
{
    public class OrderConverter
    {
        public static List<Model.Order> ConvertToModelObject(List<Entity.Order> entities)
        {
            List<Model.Order> models = new List<Model.Order>();
            foreach (Entity.Order entity in entities)
            {
                models.Add(ConvertToModelObject(entity));
            }
            return models;
        }

        public static List<Entity.Order> ConvertToEntity(List<Model.Order> models)
        {
            List<Entity.Order> entities = new List<Entity.Order>();
            foreach (Model.Order model in models)
            {
                entities.Add(ConvertToEntity(model));
            }
            return entities;
        }

        public static Model.Order ConvertToModelObject(Entity.Order entity)
        {
            if (entity == null) return null;
            Model.Order model = new Model.Order();
            model.OrderId = entity.Id;
            model.OrderDate = entity.OrderDate;
            model.OrderStatus = (OrderStatusType)Enum.Parse(typeof(OrderStatusType), entity.OrderStatus);
            model.ShippingCost = entity.ShippingCost;
            model.UserId = entity.UserId.Value;
            model.Name = entity.UserFullName;
            model.EmailAddress = entity.UserEmailAddress;
            model.PhoneNumber = entity.UserPhoneNumber;
            model.SpecialInstruction = entity.UserInstruction;

            Address shippingAddress = new Address();

            shippingAddress.Name = entity.ShippingName;
            shippingAddress.PhoneNumber = entity.ShippingPhoneNumber;
            shippingAddress.AddressLine1 = entity.ShippingAddressLine1;
            shippingAddress.AddressLine2 = entity.ShippingAddressLine2;
            shippingAddress.Zip = entity.ShippingZip;
            shippingAddress.City = entity.ShippingCity;
            shippingAddress.Country = entity.ShippingCountry;
            model.ShippingAddress = shippingAddress;

            model.ShippingStatus = (ShippingStatusType)Enum.Parse(typeof(ShippingStatusType), entity.ShippingStatus);
            model.PaymentType = (PaymentType)Enum.Parse(typeof(PaymentType), entity.PaymentType);
            model.TransectionId = entity.PaymentTrasectionId;
            model.PaymentStatus = entity.PaymentStatus;
            model.OrderNumber = entity.OrderNumber;
            model.SubTotal = entity.SubTotal;
            model.ShippingCost = entity.ShippingCost;
            return model;
        }

        public static Entity.Order ConvertToEntity(Model.Order model)
        {
            Entity.Order entity = new Entity.Order();

            entity.Id = model.OrderId;
            entity.OrderDate = model.OrderDate;
            entity.OrderStatus = model.OrderStatus.ToString();
            entity.ShippingCost = model.ShippingCost;

            if (model.UserId == 0)
                entity.UserId = null;
            else
                entity.UserId = model.UserId;
            entity.UserFullName = model.Name;
            entity.UserEmailAddress = model.EmailAddress;
            entity.UserPhoneNumber = model.PhoneNumber;
            entity.UserInstruction = model.SpecialInstruction;
            entity.SubTotal = model.SubTotal;
            entity.OrderAmount = model.TotalAmount;
            if (model.ShippingAddress != null)
            {
                entity.ShippingName = model.ShippingAddress.Name;
                entity.ShippingPhoneNumber = model.ShippingAddress.PhoneNumber;
                entity.ShippingAddressLine1 = model.ShippingAddress.AddressLine1;
                entity.ShippingAddressLine2 = model.ShippingAddress.AddressLine2;
                entity.ShippingZip = model.ShippingAddress.Zip;
                entity.ShippingCity = model.ShippingAddress.City;
                entity.ShippingCountry = model.ShippingAddress.Country;
                entity.ShippingStatus = model.ShippingStatus.ToString();
            }

            entity.PaymentType = model.PaymentType.ToString();
            entity.PaymentTrasectionId = model.TransectionId;
            entity.PaymentStatus = model.PaymentStatus.ToString();
            entity.OrderNumber = model.OrderNumber;
            return entity;
        }
    }
}
